<?php
declare (strict_types=1);
use think\facade\Request;
use think\facade\Db;
// 应用公共函数库文件

/**
 * 获取当前访问的渠道(微信小程序、H5、APP等)
 * @return string|null
 */
function getPlatform(): ?string
{
    static $value = null;
    // 从header中获取 channel
    empty($value) && $value = request()->header('platform');
    // 调试模式下可通过param中获取
    if (is_debug() && empty($value)) {
        $value = request()->param('platform');
    }
    return $value;
}

function throwError(string $message, int $code = null) {
    $code = $code ?? config('status.error');
    throw new \think\Exception($message, $code);
}

function picReplace(string $str): string
{
    $res = str_replace(['"../static','"/static'], '"'.Request::domain().'/static', $str);
    $res = str_replace(['"../upload','"/upload'], '"'.Request::domain().'/upload', $res);
    return $res;
}